package com.winchain.chainbill.bill.biz.receive.service.impl;

import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.asset.biz.credit.rx.BillAssetTranSubscriber;
import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.service.CbRcvService;
import com.winchain.chainbill.bill.biz.receive.rx.BillRcvArpvPassSubscriber;
import com.winchain.chainbill.bill.biz.receive.rx.BillRcvArpvRjctSubscriber;
import com.winchain.chainbill.bill.biz.receive.rx.BillRcvDenySubscriber;
import com.winchain.chainbill.bill.biz.receive.service.BillRcvAprvService;
import com.winchain.chainbill.bill.biz.receive.vo.BillRcv;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("billRcvAprvService")
public class BillRcvAprvServiceImpl implements BillRcvAprvService {
	@Autowired
	private CbRcvService cbRcvService;
	@Autowired
	private BillRcvArpvPassSubscriber billInputArpvPassSubscriber;
	@Autowired
	private BillRcvArpvRjctSubscriber billInputArpvRjctSubscriber;
	@Autowired
	private BillRcvDenySubscriber billRcvDenySubscriber;
	@Autowired
	private BillAssetTranSubscriber billAssetTranSubscriber;

	public BatchBillStats<CbRcv> pass(String phoneCode, String location, String... billIds) {
		List<BillRcv> records = this.cbRcvService.getBillRcvByStateAndIds("1", billIds);
		BatchBillEvent<BillRcv, CbRcv> event = new BatchBillEvent();
		event.setRecs(records);

		CbOpt opt = new CbOpt();
		opt.setCurState("S025");
		opt.setOptCode("E006");
		opt.setRemark("������������");

		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("E004");
		timeoutOpt.setRemark("������������,������������");

		BatchBillStats<CbRcv> stats = new BatchBillStats();

		event.setOperation(opt);
		event.setTimeoutOpt(timeoutOpt);
		event.setStats(stats);
		event.setValue("phoneCode", phoneCode);
		event.setValue("location", location);

		Observable<BatchBillEvent<BillRcv, CbRcv>> observers = Observable.just(event);

		observers.subscribe(this.billInputArpvPassSubscriber);
		observers.subscribe(this.billAssetTranSubscriber);
		if (ChainBillUtil.onChain()) {
			Object txSnHost = event.getValue("txSnHost");
			if (txSnHost != null) {
				stats.setTxSnHost((String) txSnHost);
			}
		}
		if ((records == null) || (records.size() == 0)) {
			stats.addAbnormalMsg("������������������������");
		} else if (billIds.length > records.size()) {
			stats.addAbnormalMsg("����������������������������");
		}
		return stats;
	}

	public BatchBillStats<CbRcv> reject(String remark, String... billIds) {
		List<BillRcv> records = this.cbRcvService.getBillRcvByStateAndIds("1", billIds);
		BatchBillEvent<BillRcv, CbRcv> event = new BatchBillEvent();
		event.setRecs(records);

		CbOpt opt = new CbOpt();
		opt.setCurState("S025");
		opt.setOptCode("E007");

		opt.setRemark(remark);

		BatchBillStats<CbRcv> stats = new BatchBillStats();

		event.setOperation(opt);
		event.setStats(stats);
		Observable<BatchBillEvent<BillRcv, CbRcv>> observers = Observable.just(event);

		observers.subscribe(this.billInputArpvRjctSubscriber);
		observers.subscribe(this.billAssetTranSubscriber);
		if ((records == null) || (records.size() == 0)) {
			stats.addAbnormalMsg("������������������������");
		} else if (billIds.length > records.size()) {
			stats.addAbnormalMsg("����������������������������");
		}
		return stats;
	}

	public BatchBillStats<CbRcv> expReject(String remark, String... billIds) {
		List<BillRcv> records = this.cbRcvService.getBillRcvByStateAndIds("1", billIds);
		BatchBillEvent<BillRcv, CbRcv> event = new BatchBillEvent();
		event.setRecs(records);

		CbOpt opt = new CbOpt();
		opt.setCurState("S025");

		opt.setOptCode("E004");
		opt.setRemark(remark);

		BatchBillStats<CbRcv> stats = new BatchBillStats();

		event.setOperation(opt);
		event.setStats(stats);
		event.setValue("bizType", "cb_rcv");
		event.setValue("exp", Boolean.valueOf(true));

		Observable<BatchBillEvent<BillRcv, CbRcv>> observers = Observable.just(event);
		observers.subscribe(this.billRcvDenySubscriber);

		observers.subscribe(this.billAssetTranSubscriber);

		return stats;
	}
}
